<template>
  <uni-forms
    class="login-form"
    ref="form"
    :rules="accountRules"
    :model="formData"
  >
    <uni-forms-item name="account">
      <input
        type="text"
        v-model="formData.account"
        placeholder="请输入账号"
        class="uni-input-input"
        placeholder-style="color: #818181"
      />
    </uni-forms-item>
    <uni-forms-item name="password">
      <input
        v-model="formData.password"
        type="text"
        placeholder="请输入密码"
        class="uni-input-input"
        placeholder-style="color: #818181"
      />
    </uni-forms-item>
    <button class="submit-button" @click="onFormSubmit">登录</button>
  </uni-forms>
</template>

<script>
import { login } from '@/utils/user.js'
export default {
  data () {
    return {
      formData: {
        account: 'xbsj001',
        password: '123456',
      },
      accountRules: {
        account: {
          rules: [
            { required: true, errorMessage: '请输入登录账号' },
            {
              pattern: '^[a-zA-Z0-9]{6,8}$',
              errorMessage: '登录账号格式不正确',
            },
          ],
        },
        password: {
          rules: [
            { required: true, errorMessage: '请输入登录密码' },
            { pattern: '^\\d{6}$', errorMessage: '登录密码格式不正确' },
          ],
        },
      },
    }
  },

  methods: {
    async onFormSubmit () {
      try {
        // 验证通过
        await this.$refs.form.validate()
        // const res = await login(this.formData)
        await this.$store.dispatch('user/login', this.formData)
        // console.log(res)
        // setTimeout(() => {
        //   uni.navigateBack()
        // }, 1500)
        uni.switchTab({
          url: '/pages/index/index',
        })

        // 表单的数据
      } catch (err) {
        // 验证失败
        console.log(err)
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.uni-forms-item {
  height: 100rpx;
  margin-bottom: 20 !important;
  border-bottom: 2rpx solid #eee;
  box-sizing: border-box;
}

::v-deep .uni-forms-item__content {
  display: flex;
  align-items: center;
}

::v-deep input {
  width: 100%;
  font-size: 14px;
  color: #2a2929;
}

::v-deep .uni-forms-item__error {
  width: 100%;
  padding-top: 10rpx;
  border-top: 2rpx solid #ef4f3f;
  color: #ef4f3f;
  font-size: 28rpx;
  transition: none;
}

.text-button {
  width: 300rpx;
  padding-left: 10rpx;
  text-align: center;
  font-size: 28rpx;
  color: 28rpx;
  border-left: 2rpx solid #eee;
}

.submit-button {
  height: 100rpx;
  line-height: 100rpx;
  /* #ifdef APP */
  padding-top: 4rpx;
  /* #endif */
  margin-top: 80rpx;
  border: none;
  color: #fff;
  background-color: #ef4f3f;
  border-radius: 100rpx;
  font-size: 36rpx;
}

button[disabled] {
  background-color: #fadcd9;
  color: #fff;
}
</style>
